#!/usr/bin/env python

from gradelib import *

r = Runner(save("jos.out"),
           stop_breakpoint("readline"))

def matchtest(parent, name, *args, **kw):
    def do_test():
        r.match(*args, **kw)
    test(5, name, parent=parent)(do_test)

@test(0, "internal FS tests [fs/test.c]")
def test_fs():
    r.user_test("hello")
matchtest(test_fs, "fs i/o",
          "FS can do I/O",
          no=["idle loop can do I/O"])
matchtest(test_fs, "check_bc",
          "block cache is good")
matchtest(test_fs, "check_super",
          "superblock is good")
matchtest(test_fs, "check_bitmap"
          "bitmap is good")
matchtest(test_fs, "alloc_block",
          "alloc_block is good")
matchtest(test_fs, "file_open",
          "file_open is good")
matchtest(test_fs, "file_get_block",
          "file_get_block is good")
matchtest(test_fs, "file_flush/file_truncate/file rewrite",
          "file_flush is good",
          "file_truncate is good",
          "file rewrite is good")

@test(0)
def test_testfile():
    r.user_test("testfile")
matchtest(test_testfile, "serve_open/file_stat/file_close",
          "serve_open is good",
          "file_stat is good",
          "file_close is good",
          "stale fileid is good")
matchtest(test_testfile, "file_read",
          "file_read is good")
matchtest(test_testfile, "file_write",
          "file_write is good")
matchtest(test_testfile, "file_read after file_write",
          "file_read after file_write is good")
matchtest(test_testfile, "open",
          "open is good")
matchtest(test_testfile, "large file",
          "large file is good")

@test(10, "motd display [writemotd]")
def test_writemotd1():
    r.user_test("writemotd", snapshot=False)
    r.match("OLD MOTD",
            "This is /motd, the message of the day.",
            "NEW MOTD",
            "This is the NEW message of the day!")

@test(10, "motd change [writemotd]", parent=test_writemotd1)
def test_writemotd2():
    try:
        r.user_test("writemotd")
        r.match("OLD MOTD",
                "This is the NEW message of the day!",
                "NEW MOTD",
                no=["This is /motd, the message of the day."])
    finally:
        reset_fs()

@test(15, "spawn via icode [icode]")
def test_icode():
    r.user_test("icode")
    r.match("icode: read /motd",
            "This is /motd, the message of the day.",
            "icode: spawn /init",
            "init: running",
            "init: data seems okay",
            "icode: exiting",
            "init: bss seems okay",
            "init: args: 'init' 'initarg1' 'initarg2'",
            "init: exiting")

run_tests()
